package net.hn.hnms.biz.upkafka.config;

import com.alibaba.druid.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public final class KafkaProperties
{
    private static final Logger LOG = LoggerFactory.getLogger(KafkaProperties.class);
    
    // Topic名称，安全模式下，需要以管理员用户添加当前用户的访问权限
    public final static String TOPIC = "example-metric1";
    //综采实时 topic
    public final static String ZCXT_ZCSS_TOPIC = "zcxt_zcss";
    //综采实时topic_tmp
    public final static String ZCXT_ZCSS_TOPIC_tmp = "zcxt_zcss_tmp";
    //主通风实时
    public final static String ZTF_ZTFSS_TOPIC = "ztf_tfss";
    //主排水实时
    public final static String ZTF_ZPSSS_TOPIC = "zps_psss";
    //斜井实时
    public final static String ZTF_XJSS_TOPIC = "xjts_xjss";

    //空压机实时
    public final static String KYJ_YFSS_TOPIC = "kyj_kyss";

    //安全监测  测点基本信息
    public final static String AQJC_CDDY_TOPIC = "aqjc_cddy";
    //安全监测  分站基本信息
    public final static String AQJC_FZDY_TOPIC = "aqjc_fzdy";
    //安全监测  测点实时数据
    public final static String AQJC_CDSS_TOPIC = "aqjc_cdss";
    //安全监测  测点统计数据
    public final static String AQJC_TJSJ_TOPIC = "aqjc_tjsj";
    //安全监测  测点异常数据
    public final static String AQJC_YCBJ_TOPIC = "aqjc_ycbj";
    //安全监测  分站实时
    public final static String AQJC_FZSS_TOPIC = "aqjc_fzss";

    //人员定位  区域基本信息
    public final static String RYDW_RYQY_TOPIC = "rydw_ryqy";
    //人员定位  基站基本信息
    public final static String RYDW_RYJZ_TOPIC = "rydw_ryjz";
    //人员定位  人员基本信息
    public final static String RYDW_RYXX_TOPIC = "rydw_ryxx";
    //人员定位  井下作业人员实时数据
    public final static String RYDW_RYSS_TOPIC = "rydw_ryss";
    //人员定位  超时报警
    public final static String RYDW_RYCS_TOPIC = "rydw_rycs";
    //人员定位  超员报警
    public final static String RYDW_RYCY_TOPIC = "rydw_rycy";
    //人员定位  进入限制区域报警
    public final static String RYDW_RYXZ_TOPIC = "rydw_ryxz";
    //人员定位  人员求救
    public final static String RYDW_RYQJ_TOPIC = "rydw_ryqj";
    //人员定位  基站实时数据
    public final static String RYDW_JZSS_TOPIC = "rydw_jzss";



    /**
     * 用户自己申请的机机账号keytab文件名称
     */
    public static final String USER_KEYTAB_FILE_linglu = "user.keytab";

    /**
     * 用户自己申请的机机账号keytab文件名称
     */
    public static final String USER_KEYTAB_FILE = "user.keytab";

    /**
     * 用户自己申请的机机账号名称
     */
    public static final String USER_PRINCIPAL = "ll_mk";

    /**
     * 用户自己申请的机机账号名称 灵露
     */
    public static final String USER_PRINCIPAL_LINGLU = "ll_mk";
    /**
     * 高头窑
     */
    public static final String USER_PRINCIPAL_GAOTOUYAO = "gty_mk";
    /**
     * 研北
     */
    public static final String USER_PRINCIPAL_YANBEI = "ll_mk";
    private static Properties serverProps = new Properties();

    private static Properties producerProps = new Properties();

    private static Properties consumerProps = new Properties();
    
    private static Properties clientProps = new Properties();
    
    private static KafkaProperties instance = null;
    private static Map<String, String> mineMap = new HashMap<>();

    private KafkaProperties()
    {
//        String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator;
//        String filePath = System.getProperty("user.dir") +File.separator+"hnms-biz-modules"+File.separator+"hnms-biz-production"+File.separator+"hnms-biz-mining"+ File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator;

//        String filePath = KafkaProperties.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String filePath = SpringContextUtil.getBean(ResourceExtractor.class).getTempDir().toAbsolutePath().toString()+File.separator;

//        String osName = System.getProperty("os.name").toLowerCase();
//        if (osName.contains("win")) {
//            // windows路径下分隔符替换
//        } else if (osName.contains("nux") || osName.contains("nix")) {
//            // 指定文件夹路径
//        }
        System.out.println("KafkaProperties-filePath = " + filePath);
        try
        {
            File proFile = new File(filePath + "producer.properties");
        
            if (proFile.exists())
            {
                producerProps.load(new FileInputStream(filePath + "producer.properties"));
            }
        
            File conFile = new File(filePath + "producer.properties");
        
            if (conFile.exists())
            {
//                consumerProps.load(new FileInputStream(filePath + "consumer.properties"));
            }
        
            File serFile = new File(filePath + "server.properties");
            
            if (serFile.exists())
            {
                serverProps.load(new FileInputStream(filePath + "server.properties"));
            }

            File cliFile = new File(filePath + "client.properties");
            
            if (cliFile.exists())
            {
                clientProps.load(new FileInputStream(filePath + "client.properties"));
            }
            File mineFile = new File(filePath + "mine.properties");
            // 判断文件是否存在
            if (mineFile.exists()) {
                // 使用 InputStreamReader 指定 UTF-8 编码读取文件
                try (FileInputStream fileInputStream = new FileInputStream(mineFile);
                     InputStreamReader reader = new InputStreamReader(fileInputStream, "UTF-8")) {

                    Properties properties = new Properties();
                    properties.load(reader);  // 从 reader 而不是 inputStream 读取

                    // 将Properties中的键值对提取并放入 Map
                    for (String key : properties.stringPropertyNames()) {
                        mineMap.put(key, properties.getProperty(key));
                    }

                    System.out.println("Properties loaded into MineMap: " + mineMap);
                } catch (IOException e) {
                    System.err.println("Error reading properties file: " + e.getMessage());
                }
            } else {
                throw new RuntimeException("File not found: " + filePath + "mine.properties");
            }
        }
        catch (IOException e)
        {
            LOG.info("The Exception occured.", e);
        }
    }
    
    public synchronized static KafkaProperties getInstance()
    {
        if (null == instance)
        {
            instance = new KafkaProperties();
        }
        
        return instance;
    }

    /**
    * 获取参数值
    * @param key properites的key值
    * @param defValue 默认值
    * @return
    */
    public String getValues(String key, String defValue)
    {
        String rtValue = null;
        
        if (null == key)
        {
            LOG.error("key is null");
        }
        else
        {
            rtValue = getPropertiesValue(key);
        }
        
        if (null == rtValue)
        {
            LOG.warn("KafkaProperties.getValues return null, key is " + key);
            rtValue = defValue;
        }
        
        LOG.info("KafkaProperties.getValues: key is " + key + "; Value is " + rtValue);
        
        return rtValue;
    }
    
    /**
    * 根据key值获取server.properties的值
    * @param key
    * @return
    */
    private String getPropertiesValue(String key)
    {
        String rtValue = serverProps.getProperty(key);
        
        // server.properties中没有，则再向producer.properties中获取
        if (null == rtValue)
        {
            rtValue = producerProps.getProperty(key);
        }
        
        // producer中没有，则再向consumer.properties中获取
//        if (null == rtValue)
//        {
//            rtValue = consumerProps.getProperty(key);
//        }
        
        // consumer没有，则再向client.properties中获取
        if (null == rtValue)
        {
            rtValue = clientProps.getProperty(key);
        }
        
        return rtValue;
    }
    // 访问静态 Map 的方法
    public static Map<String, String> getMineMap() {
        return mineMap;
    }

    public static String getMineNameByMineCode(String mineCode) {
        String mineName = mineMap.get(mineCode);
        if (StringUtils.isEmpty(mineName)) {
            return "---";
        }
        return mineName;
    }
}
